package org.oproject.rmi.hessian.core.job;

import java.io.Serializable;
import java.util.Arrays;

import org.oproject.rmi.hessian.core.context.ContextUtil;

/**
 * 远程调用hessian接口的JOB封装类
 * @author SQ
 *
 */
public class HessianJob implements Serializable
{
	private static final long serialVersionUID = 1L;
	/**
	 * 调用的bean名称
	 */
	private String serviceName;
		
	/**
	 * 调用的参数数组
	 */
	private Object[] args;
	
	/**
	 * 参数类型，主要用于区分基础类型和封装类型比如基础类型的int，需要的类型为Integer.TYPE
	 */
//	@SuppressWarnings("unchecked")
//	private Class[] argsType;
	
	/**
	 * 时间戳
	 */
	private long time = System.currentTimeMillis();
	
	/**
	 * 唯一的JOB id，在客服端和服务器端保持一致
	 */
	private String jobID = ContextUtil.generateJobID();
	
	/**
	 * 同时生成下一次调用的时候才用的JOB ID
	 */
	private String nextJobID = ContextUtil.generateJobID();
	
	/**
	 * 是否支持自动刷新，默认情况下支持
	 */
	private transient boolean autoFreshAble = true;
		
	public HessianJob(){}
	
	public HessianJob(boolean autoFreshAble)
	{
		this.autoFreshAble = autoFreshAble;
	}
	
	@Override
	public String toString()
	{
		StringBuffer str = new StringBuffer();
		str.append("jobID:" + jobID);
		str.append("; ");
		str.append("serviceName:" + serviceName);
		str.append("; ");
		str.append("args:" + Arrays.asList(args));
		str.append("; ");
		str.append("time:" + time);
		str.append("; ");
		return str.toString();
	}
	
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(args);
		result = prime * result
				+ ((serviceName == null) ? 0 : serviceName.hashCode());
		result = prime * result + (int) (time ^ (time >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final HessianJob other = (HessianJob) obj;
		if (!Arrays.equals(args, other.args))
			return false;
		if (serviceName == null)
		{
			if (other.serviceName != null)
				return false;
		}
		else if (!serviceName.equals(other.serviceName))
			return false;
		if (time != other.time)
			return false;
		return true;
	}

	/**
	 * 获取被调用的bean名称
	 * @return
	 */
	public String getServiceName()
	{
		return serviceName;
	}

	/**
	 * 设置被调用的bean名称
	 * @param beanName
	 */
	public void setServiceName(String serviceName)
	{
		this.serviceName = serviceName;
	}

	/**
	 * 获取调用方法的参数列表
	 * @return
	 */
	public Object[] getArgs()
	{
		return args;
	}

	/**
	 * 设置调用方法的参数列表
	 * @param args
	 */
	public void setArgs(Object[] args)
	{
		this.args = args;
	}
/*
	@SuppressWarnings("unchecked")
	public Class[] getArgsType() {
		return argsType;
	}

	@SuppressWarnings("unchecked")
	public void setArgsType(Class[] argsType) {
		this.argsType = argsType;
	}
*/
	/**
	 * 获取时间戳
	 */
	public long getTime()
	{
		return time;
	}
	
	public boolean validate()
	{
		if(null == this.serviceName || serviceName.trim().equals(""))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public String getJobID()
	{
		return this.jobID;
	}
	
//	public void setJobID(String _jobID)
//	{
//		this.jobID = _jobID;
//	}
	
	public boolean isAutoFreshAble()
	{
		return autoFreshAble;
	}

	public void setAutoFreshAble(boolean autoFreshAble)
	{
		this.autoFreshAble = autoFreshAble;
	}
	
	/**
	 * 建议每次使用该类，都调用一次该方法，生成新的job id，方便日志定位
	 */
	public void fresh()
	{
		this.jobID = nextJobID;
		this.nextJobID = ContextUtil.generateJobID();
	}
}
